package com.hpu.tools;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;

public class InternetTools {

    /**
     * 没有网络
     */
    public static final int NO_INTERNET = 0;

    /**
     * 连接到网络internet
     */
    public static final int INTERNET = 1;

    /**
     * 连接到wifi
     */
    public static final int WIFI = 2;

    /**
     * 获取网络状态
     *
     * @param context 上下文
     * @return 返回常量
     */
    public static int statusInternet(Context context) {
        ConnectivityManager con = (ConnectivityManager) context.getSystemService(Activity.CONNECTIVITY_SERVICE);
        boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
        boolean internet = con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();

        if (wifi) {
            if (isOk())
                return WIFI;
            else return NO_INTERNET;
        }

        if (internet) {
            if (isOk())
                return INTERNET;
            else return NO_INTERNET;
        }

        return NO_INTERNET;
    }

    public static boolean isOk() {
        return true;
//        Runtime runtime = Runtime.getRuntime();
//        try {
//            Process p = runtime.exec("ping -c 3 www.baidu.com");
//            int ret = p.waitFor();
//            if (ret == 0) return true;
//        } catch (Exception e) {
//            return false;
//        }
//
//        return false;
    }
}
